/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.swing;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Francisco Morero Peyrona
 */

public class JxDialog extends JDialog
{
   public final static int ACCEPTED  = JOptionPane.OK_OPTION;
   public final static int CANCELLED = JOptionPane.CANCEL_OPTION;

   protected JPanel  pnlButtons = new JPanel( new FlowLayout( FlowLayout.RIGHT, 9, 0 ) );
   protected JButton btnAccept  = new JButton( "Accept" );
   protected JButton btnCancel  = new JButton( "Cancel" );
   protected JButton btnHelp    = new JButton( "Help" );

   private int nClosedOption = CANCELLED;

   //-------------------------------------------------------------------------//

   public JxDialog()
   {
      this( "" );
   }

   public JxDialog( String sTitle )
   {
      super( jdbcmanager.app.App.getFrameMain(), sTitle, true );

      setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
             
      getContentPane().setLayout( new BorderLayout() );
      
      if( getContentPane() instanceof JPanel )
         ((JPanel) getContentPane()).setBorder( BorderFactory.createEmptyBorder( 9,9,9,9 ) );
      
      initButtonPanel();
   }

   /**
    * Return close option value: either JxDialog.ACCEPTED or JxDialog.CANCELLED 
    * 
    * @return Close option value
    */
   public int getCloseOption()
   {
      return this.nClosedOption;
   }

   /**
    * Performs pack() using a minimum and maximum width and height sizes.
    * <p>
    * Paramaters admited values:
    * <ul>
    *    <li>Negative value --> minimum value
    *    <li>Zero           --> ignore  value
    *    <li>Positive value --> maxumum value
    * </ul>
    * 
    * @param nWidth Minimum or Maximum width
    * @param nHeight Minimum or Maximum height
    */
   public void pack( int nWidth, int nHeight )
   {
      super.pack();
      
      if( nWidth == 0 )
         nWidth = getWidth();
      else if( nWidth < 0 )
         nWidth = Math.max( getWidth(), nWidth * -1 );
      else
         nWidth = Math.min( getWidth(), nWidth );
      
      if( nHeight == 0 )
         nHeight = getHeight();
      else if( nHeight < 0 )
         nHeight = Math.max( getHeight(), nHeight * -1 );
      else
         nHeight = Math.min( getHeight(), nHeight );
      
      setSize( nWidth, nHeight );
   }   

   /**
    * Centers the dialog relative Application Main Frame.
    */
   public void center()
   {
      center( null );   // null because all JxDialog has as their parent frmMain (see this class constructor)
   }

   /**
    * Centers the dialog relative passed parent
    * @param parent
    */
   public void center( Container parent )
   {
      if( parent == null )
         parent = getOwner();
      
      setLocationRelativeTo( parent );
   }
   
   /**
    * Hides /shows button panel
    * @param b
    */
   public void setVisibleButtonPanel( boolean b )
   {
      this.pnlButtons.setVisible( b );
   }
   
   public boolean isVisibleButtonPanel()
   {
      return this.pnlButtons.isVisible();
   }

   /**
    * Can be redefined by sub-classes
    *
    * @param evt
    */
   protected void onAccept( ActionEvent evt )
   {
      this.nClosedOption = ACCEPTED;
      dispose();
   }
   
   /**
    * To be redefined by sub classes if making button Help visible.
    */
   protected void onHelp( ActionEvent evt )
   {
   }

   /**
    * Can be redefined by sub-classes
    *
    * @param evt
    */
   protected void onCancel( ActionEvent evt )
   {
      this.nClosedOption = CANCELLED;
      dispose();
   }

   //-------------------------------------------------------------------------//

   private void initButtonPanel()
   {
      pnlButtons.setBorder( BorderFactory.createEmptyBorder( 9,0,0,0 ) );
      
      getRootPane().setDefaultButton( btnAccept );

      btnAccept.setMnemonic( 'A' );
      btnAccept.setMultiClickThreshhold( 2000 );
      btnCancel.setMnemonic( 'C' );    
      btnHelp.setMnemonic( 'H' );
      btnHelp.setVisible( false );    // By default it is hidden
      
      pnlButtons.add( btnAccept );
      pnlButtons.add( btnCancel );
      pnlButtons.add( btnHelp );

      getContentPane().add( pnlButtons, BorderLayout.SOUTH );
      
      //-------------------------------------------------------------------//

      btnAccept.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent evt )
         {
            onAccept( evt );
         }
      } );

      btnCancel.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent evt )
         {
            onCancel( evt );
         }
      } );
      
     btnHelp.addActionListener( new ActionListener()
     {
        public void actionPerformed( ActionEvent evt )
        {
           onHelp( evt );
        }
     } );
   }
}